package com.leemz.ch2.jdbc.firstedition;

import com.leemz.ch2.jdbc.entity.Student;

import java.sql.*;

/**
 * jdbc第一版实现，只考虑功能实现不考虑后期维护，夹杂有大量重复和样板代码
 */
public class StudentService {
    public void save(Student student) {
        String sql = "insert into t_student(name, age) values(?,?)";
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring_core?serverTimezone=UTC", "root", "lixiao");
            pstmt = connection.prepareStatement(sql);
            pstmt.setObject(1, student.getName());
            pstmt.setObject(2, student.getAge());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if(connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
            }
        }
    }

    public void delete(int id) {
        String sql = "delete from t_student where id = ?";
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring_core?serverTimezone=UTC", "root", "lixiao");
            pstmt = connection.prepareStatement(sql);
            pstmt.setObject(1, id);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    public void update(Student student) {
        String sql = "update t_student set name = ?, age = ? where id = ?";
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring_core?serverTimezone=UTC", "root", "lixiao");
            pstmt = connection.prepareStatement(sql);
            pstmt.setObject(1, student.getName());
            pstmt.setObject(2, student.getAge());
            pstmt.setObject(3, student.getId());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
